// ProjectName: go-learn-demo
// PackageName: _1_basic
// Description:
// Author: hy.bai
// Date: 2025-08-19 星期二 11:44

package xormybxb

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"log/slog"
)

// 新增操作
func TestInsert() {
	// 初始化Mysql数据库引擎
	InitEngine()

	val := &User{
		Username: "ybxb-" + currentTimeStr,
		Password: "root",
		Age:      18,
	}

	result, err := MysqlEngine.Table("t_user").Insert(val)
	if err != nil {
		slog.Error("插入数据失败,原因是:", err.Error())
	}
	fmt.Println("插入结果:", result)
	fmt.Println("回填主键是:", val.Id)
}

func TestBatchInsert() {
	// 初始化Mysql数据库引擎
	InitEngine()

	users := []User{
		{
			Username: "abc-" + currentTimeStr,
			Password: "123456",
			Age:      20,
		},
		{
			Username: "ybxb-" + currentTimeStr,
			Password: "123456",
			Age:      18,
		},
	}

	result, err := MysqlEngine.Table("t_user").Insert(&users)
	if err != nil {
		slog.Error("插入数据失败,原因是:", err.Error())
	}
	fmt.Println("插入结果:", result)
}
